import uniFly from 'unifly'
import utils from '../common/utils.js'
import state from '../store/state.js'
import config from '../common/config.js'
import md5 from '../common/md5.js'
//基础路由
uniFly.baseUrl = state.baseApiUrl
//APICloud签名校验
let now = Date.now()
let appId = "application/x-www-form-urlencoded"
// let appKey = setHeaders();
// appKey = utils.SHA1(appId + 'UZ' + appKey + 'UZ' + now) + '.' + now
// //设置请求头
uniFly.headers['Content-Type'] = appId
// uniFly.headers['cookie'] = appKey
//全局请求超时时间
uniFly.timeOut = 20000
//自定义请求拦截
uniFly.requestInterceptors.success = function(request) {
	console.log('自定义请求拦截')
	//配置基本信息
	request.headers = uniFly.headers
	uni.showLoading()
	return request
}
uniFly.responseInterceptors.success = function(request) {
	uni.hideLoading()
	return Promise.resolve(request)
};

function setHeaders() {
	var openid = uni.getStorageSync('openid');
	var session = uni.getStorageSync('session');
	var cookie = [];
	if (session) cookie.push(session)
	if (openid) cookie.push('openid=' + openid)
	// #ifdef MP-ALIPAY
	uniFly.headers['set-cookie'] = cookie.join(';');
	// #endif
	// #ifdef MP-WEIXIN
		uniFly.headers['cookie'] = cookie.join(';');
	// #endif
    console.log(uniFly.headers['cookie']);
	// return {
	//   //'content-type': 'application/x-www-form-urlencoded',
	//   'Cookie': cookie.join(';')
	// }
}

function ua() {
	var _ua = [];
	var token = 0;
	var key = config.PUB_KEY;
	var packname = config.PACK_NAME;
	var uid = uni.getStorageSync('uid');
	var openid = uni.getStorageSync('openid');
	if (uid) {
		token = md5.hex_md5(uid + "|" + uid + "|" + packname + "|" + key);
		//md5.hexMD5(uid + "|" + uid + "|" + packname + "|" + key);
	}
	var systeminfo = uni.getSystemInfoSync()
	_ua.push('x');
	_ua.push(systeminfo.system);
	_ua.push('0');
	_ua.push(config.VERSION);
	_ua.push(packname);
	_ua.push(openid);
	_ua.push(systeminfo.screenWidth);
	_ua.push(systeminfo.screenHeight);
	_ua.push(0);
	_ua.push(systeminfo.language);
	_ua.push(token);
	return '?_ua=' + _ua.join('|');
}

const $api_base = {
            
            init: '/weiapi/init',
            homeIndex: '/home/index_v2',
            homeIndex_v3: '/home/index_v3',
            reg: '/reg/i_reg_account',
            getCode:'/home/get_code',
            bindPhone:'/home/bind_phone',
            storeInfo: '/store/get_store_info_v2',
            storeList: '/store/get_store_list',
            storeSearch: '/store/get_store_search',
            storeSearchInit: '/store/init_store_search',
            goodsList: '/goods/get_goods_ByCid_v3',
            goodsListV4: '/goods/get_goods_ByCid_v4',
            goodsListV5: '/goods/get_goods_ByCid_v5',
            saveCartInfo: '/shopping_cart/save_cart_info',
            saveAddress: '/address/update_address',
            deleteAddress: '/address/delete_address',
            getAddress: '/address/get_address',
            activeAddress: '/address/active_address',
            getAddressByid: '/address/get_address_byid',
            getAddressActive: '/address/get_address_active',
            getNumList: '/address/get_num_list',
            addOrder: '/order/add_order_v2',
            payIndex: '/pay/index',
            payConfirm: '/pay/confirm',
            nomoneyPay: '/pay/nomoney_pay',
            getOrderByid: '/order/get_order_v2',
            getOrderByidV3: '/order/get_order_v3',
            getOrderList: '/order/order_list',
            feedback: '/home/feedback',
            ad: '/home/ad',
            settlementOrder: '/order/settlement_order',
            getGroupList: '/group/index',
            getGroupInfo: '/group/get_group_info',
            getHistoryGroupOrder:'/group/get_history_group_order',
            getSelfTime:'/order/get_self_time',
            getOneselfList:'/store/get_oneself_list',
            getDecryptData:'/weiapi/get_decryptData',
            getAliDecryptData:'/weiapi/get_ali_decryptData',
            storeCertification: '/store/get_store_certification',
            getUser:'/home/update_user_info',
            submitGroup:'/goods/submit_group',
            getTime:'/store/get_time',
            getKeywords: '/goods/get_keywords',
            getHistoryWords: '/goods/get_history_words',
            delHistoryWords: '/goods/del_history_words',
            goodsSearch: '/goods/goods_search',
            getGoodsActive:'/goods/get_goods_active',
            
            
            alipayInit: '/student/amp/auth-code',
            createGroup: '/student/order/create-group',     //创建拼单
            cancelGroup: '/student/order/cancel-group',
            outGroup: '/student/order/out-group',
            groupInfo: '/student/order/group-info',
            getGroupOrderGoods: '/goods/get_group_order_goods',
            confirmGoods: '/student/order/confirm-goods',  //  确定/修改拼单商品
            changeStatus: '/student/order/change-status',  //  参与人确认拼单
            checkGroup: '/student/order/check-group',  //发起人检查是否全部确认
            getGroupId: '/student/order/groupid',
            groupOpen: '/student/order/group-open',
            
            getRedpackeNew: '/redpacket/get_redpacket_new',  //弹出红包
            getRedpacketList: '/redpacket/get_redpacket_list',  //红包列表
            storeRedpacketList: '/redpacket/store_redpacket_list', //店铺红包列表
            luckeyRedpacketList: '/redpacket/lucky_redpacket_list', //手气红包列表
            getStoreRedpacket: '/redpacket/get_store_redpacket',    //领取店铺红包
            storeEvaluationList: '/comment/store_evaluation_list',
            storeEvaluation: '/comment/store_evaluation',
            evaluationInfo: '/comment/evaluation_info',
            storeScore:'/comment/store_score',
            mineEvaluation:'/comment/mine_evaluation',
	post: function(str, params = {},mask) {
		uniFly.requestInterceptors.success = function(request) {
			//配置基本信息
			request.headers = uniFly.headers
			if(!mask){
			  uni.showLoading()			
			}
			return request
		}
		uniFly.baseUrl = state.baseApiUrl
			// #ifdef MP-WEIXIN
			setHeaders()
			// #endif
			return uniFly.post({
				url: str + ua(),
				params: params
			})
	},
	post_new: function(str, params = {},mask) {
		uniFly.requestInterceptors.success = function(request) {
			//配置基本信息
			request.headers = uniFly.headers
			if(!mask){
                uni.showLoading()			
			}
			return request
		}
		uniFly.baseUrl = state.baseApiUrl_new;
			setHeaders()
			return uniFly.post({
				url: str + ua(),
				params: params
			})
	},
    post_wpc: function(str, params = {},mask) {
    	uniFly.requestInterceptors.success = function(request) {
    		//配置基本信息
    		request.headers = uniFly.headers
    		if(!mask){
                uni.showLoading()			
    		}
    		return request
    	}
    	uniFly.baseUrl = state.baseApiUrl_wpc;
        setHeaders()
        return uniFly.post({
            url: str + ua(),
            params: params
        })
    },
    post_tw: function(str, params = {}, mask) {
    	uniFly.requestInterceptors.success = function(request) {
    		//配置基本信息
    		request.headers = uniFly.headers
    		if(!mask){
                uni.showLoading()			
    		}
    		return request
    	}
    	uniFly.baseUrl = state.baseApiUrl_tw;
        setHeaders()
        return uniFly.post({
            url: str + ua(),
            params: params
        })
    },
    post_zjd: function(str, params = {}, mask) {
    	uniFly.requestInterceptors.success = function(request) {
    		//配置基本信息
    		request.headers = uniFly.headers
    		if(!mask){
                uni.showLoading()			
    		}
    		return request
    	}
    	uniFly.baseUrl = state.baseApiUrl_zjd;
        setHeaders()
        return uniFly.post({
            url: str + ua(),
            params: params
        })
    },
	get_wpc: function(str, params = {},mask) {
        uniFly.requestInterceptors.success = function(request) {
        	//配置基本信息
        	request.headers = uniFly.headers
        	if(!mask){ 
                uni.showLoading()			
        	}
        	return request
        }
        uniFly.baseUrl = state.baseApiUrl_wpc;
        setHeaders()
        return uniFly.get({
            url: str + ua(),
            params: params
        })
	},
}

export default $api_base
